#ifndef _USE_COUNT
#define _USE_COUNT

class HasPtr;
class U_Ptr{
	friend class HasPtr;
	int *ip;
	size_t use;
	U_Ptr(int *p) : ip(p), use(1){}
	~U_Ptr(){
		delete ip;
	}
};

class HasPtr{
	U_Ptr *uptr;
	int val;
public:
	HasPtr(int *p, int i) : uptr(new U_Ptr(p)), val(i){}
	HasPtr(const HasPtr &org) : uptr(org.uptr), val(org.val){++uptr->use;}
	HasPtr& operator=(const HasPtr&);
	~HasPtr(){
		if(0 == --uptr->use)
			delete uptr;
	}
};

HasPtr& HasPtr::operator=(const HasPtr &rhs){
	++rhs.uptr->use;
	if(--uptr->use == 0)
		delete uptr;
	uptr = rhs.uptr;
	val = rhs.val;
	return *this;
}
#endif // _USE_COUNT

int main(){
	int *iptr = new int[5];
	// HasPtr& hp1(iptr, 5); // IntelliSense: a reference of type "HasPtr &" (not const-qualified) cannot be initialized with a value of type "int *"
	HasPtr hp1(new int[6], 6);
	HasPtr *hp2 = &hp1;
	HasPtr& hp3 = hp1;

	return 0;
}
